procedure others ;
 var
   c : char ;

procedure Bspline_curve ;
 type
   arry = array [1..20,1..2] of integer ;

 var
   knotk , knotn , n , k , i , j : integer ;
   arr : arry ;
   m , x , y : real ;
   x1 , y1 , x2 , y2 , flag , ctr : integer ;
   c : char ;
   kbflag : byte absolute $0040:$0017 ;

 function knot ( i : integer ) : integer ;
  begin
   if i < knotk then
     knot := 0
   else
     if i > knotn then
       knot := knotn - knotk + 2
     else
       knot := i - knotk + 1 ;
  end ;

 function nblend ( i , k : integer ; u : real ) : real ;
  var
    t:integer ;
    v:real ;
  begin
   if k = 1 then
    begin
      v := 0 ;
      if ( knot( i ) <= u ) and ( u < knot( i + 1 ) ) then
         v := 1 ;
    end
   else
    begin
      v := 0 ;
      t := knot( i + k - 1 ) - knot( i ) ;
      if t <> 0 then
        v := ( u - knot( i ) ) * nblend( i , k - 1 , u ) / t ;
      t := knot( i + k ) - knot( i + 1 ) ;
      if t <> 0 then
        v := v + ( knot( i + k ) - u ) * ( nblend( i + 1 , k - 1 , u ) / t ) ;
    end ;
    nblend := v ;
  end ;

 procedure spline( var x , y : real ; u : real ; n , k : integer ; var a : arry ) ;
  var
   i : integer ;
   b : real ;
  begin
    knotk := k ;
    knotn := n ;
    x := 0 ;
    y := 0 ;
    for i := 1 to ( n + 1 ) do
     begin
       b := nblend( i , k , u ) ;
       x := x + a[i,1] * b ;
       y := y + a[i,2] * b ;
     end ;
  end ;

 procedure drawcurve ;
  begin
    n := ctr ;
    k := 4 ;
    for i := 1 to n-1 do
       draw(arr[i,1],arr[i,2],arr[i+1,1],arr[i+1,2],3) ;
    m := n - k + 2 ;
    for i := 0 to 400 do
     begin
       spline(x,y,i/400 * m,n,k,arr) ;
       plot(round(x),round(y),3) ;
     end ;
  end ;

 begin   { bspline }
   x1 := 160 ;
   x2 := x1 ;
   y1 := 100 ;
   y2 := y1 ;
   ctr := 0 ;
   c := '0' ;
   while ( ord(c) <> 27 ) do
    begin
      plot(x2,y2,3) ;
      printcoor(x2,y2) ;
      read(kbd,c) ;
      c := upcase(c) ;
      if ( c = 'C' ) then
        drawcurve ;
      if ( c = 'E' ) then
        ctr := 0 ;
      case c of
        '8' : begin
                if ( y2 > 11 ) then
                 begin
                   if ( flag = 0 ) then
                      plot(x2,y2,0 ) ;
                   flag := 0 ;
                   y2 := y2 - 1 ;
                 end ;
              end ;
        '2' : begin
                if ( y2 < 199 ) then
                 begin
                   if ( flag = 0 ) then
                      plot(x2,y2,0 ) ;
                   flag := 0 ;
                   y2 := y2 + 1 ;
                 end ;
              end ;
        '4' : begin
                if ( x2 > 0 ) then
                 begin
                   if ( flag = 0 ) then
                      plot(x2,y2,0 ) ;
                   flag := 0 ;
                   x2 := x2 - 1 ;
                 end ;
              end ;
        '6' : begin
                if ( x2 < 319 ) then
                 begin
                   if ( flag = 0 ) then
                      plot(x2,y2,0 ) ;
                   flag := 0 ;
                   x2 := x2 + 1 ;
                 end ;
              end ;
        '7' : begin
                if ( ( y2 > 11 ) and ( x2 > 0 ) ) then
                 begin
                   if ( flag = 0 ) then
                      plot(x2,y2,0 ) ;
                   flag := 0 ;
                   y2 := y2 - 1 ;
                   x2 := x2 - 1 ;
                 end ;
              end ;
        '9' : begin
                if ( ( y2 > 11 ) and ( x2 < 319 ) ) then
                 begin
                   if ( flag = 0 ) then
                      plot(x2,y2,0 ) ;
                   flag := 0 ;
                   y2 := y2 - 1 ;
                   x2 := x2 + 1 ;
                 end ;
              end ;
        '1' : begin
                if ( ( y2 < 199 ) and ( x2 > 0 ) ) then
                 begin
                   if ( flag = 0 ) then
                      plot(x2,y2,0 ) ;
                   flag := 0 ;
                   y2 := y2 + 1 ;
                   x2 := x2 - 1 ;
                 end ;
              end ;
        '3' : begin
                if ( ( y2 < 199 ) and ( x2 < 319 ) ) then
                 begin
                   if ( flag = 0 ) then
                      plot(x2,y2,0 ) ;
                   flag := 0 ;
                   y2 := y2 + 1 ;
                   x2 := x2 + 1 ;
                 end ;
              end ;
       end ;
     if ( ord(c) = 13 ) then
      begin
        plot(x2,y2,3) ;
        ctr := ctr + 1 ;
        arr[ctr,1] := x2 ;
        arr[ctr,2] := y2 ;
        x1 := x2 ;
        y1 := y2 ;
        flag := 1 ;
      end ;
    end ;
 end ;

procedure Bezier_curve ;
 type
   arr = array[0..20,1..2] of integer ;

 var
   pts : arr ;
   i , n , tx , ty : integer ;
   x , y : real ;
   x1 , y1 , x2 , y2 , flag , ctr : integer ;
   c1 : char ;

function c ( n , i : integer ) : integer ;
 var
   j , a : integer ;
 begin
   a := 1 ;
   for j := (i+1) to n do
      a := a * j ;
   for j := 1 to (n-i) do
      a := a div j ;
   c := a ;
 end ;

function bblend ( i , n : integer ; u : real ) : real ;
 var
   j : integer ;
   v : real ;
 begin
   v := c ( n , i ) ;
   for j := 1 to i do
      v := v * u ;
   for j := 1 to ( n - i ) do
      v := v * ( 1 - u ) ;
   bblend := v ;
 end ;

procedure bezier(var x , y : real ; u : real ; n : integer ) ;
 var
   i : integer ;
   b : real ;
 begin
   x := 0 ;
   y := 0 ;
   for i := 0 to n do
    begin
      b := bblend( i , n , u ) ;
      x := x + pts[i,1] * b ;
      y := y + pts[i,2] * b ;
    end ;
 end ;

procedure move (x , y : integer ) ;
 begin
   tx := x ;
   ty := y ;
 end ;

procedure line(x , y : integer ) ;
 begin
   draw(tx,ty,x,y,3) ;
   tx := x ;
   ty := y ;
 end ;

procedure drawcurve ;
 var
   i : integer ;
   x , y : real ;
 begin
   x := 0 ;
   y := 0 ;
   for i := 0 to 200 do
    begin
      bezier( x , y , i/200 , ctr-1 ) ;
      if ( i = 0 ) then
        move(round(x),round(y))
      else
        line(round(x),round(y)) ;
    end ;
 end ;

begin  { bezier }
  x1 := 160 ;
  x2 := x1 ;
  y1 := 100 ;
  y2 := y1 ;
  tx := 0 ;
  ty := 0 ;
  ctr := 0 ;
  c1 := '0' ;
  while ( ord(c1) <> 27 ) do
   begin
    plot(x2,y2,3) ;
    printcoor(x2,y2) ;
    read(kbd,c1) ;
    c1 := upcase(c1) ;
    if ( c1 = 'C' ) then
     begin
       for i := 1 to (ctr - 1) do
          draw(pts[i,1],pts[i,2],pts[i+1,1],pts[i+1,2],3) ;
       graphwindow(0,0,319,199) ;
       drawcurve ;
     end ;
    if ( c1 = 'E' ) then
      ctr := 0 ;
    case c1 of
      '8' : begin
              if ( y2 > 11 ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 y2 := y2 - 1 ;
               end ;
            end ;
      '2' : begin
              if ( y2 < 199 ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 y2 := y2 + 1 ;
               end ;
            end ;
      '4' : begin
              if ( x2 > 0 ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 x2 := x2 - 1 ;
               end ;
            end ;
      '6' : begin
              if ( x2 < 319 ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 x2 := x2 + 1 ;
               end ;
            end ;
      '7' : begin
              if ( ( y2 > 11 ) and ( x2 > 0 ) ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 y2 := y2 - 1 ;
                 x2 := x2 - 1 ;
               end ;
            end ;
      '9' : begin
              if ( ( y2 > 11 ) and ( x2 < 319 ) ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 y2 := y2 - 1 ;
                 x2 := x2 + 1 ;
               end ;
            end ;
      '1' : begin
              if ( ( y2 < 199 ) and ( x2 > 0 ) ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 y2 := y2 + 1 ;
                 x2 := x2 - 1 ;
               end ;
            end ;
      '3' : begin
              if ( ( y2 < 199 ) and ( x2 < 319 ) ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 y2 := y2 + 1 ;
                 x2 := x2 + 1 ;
               end ;
            end ;
     end ;
   if ( ord(c1) = 13 ) then
    begin
      plot(x2,y2,3) ;
      ctr := ctr + 1 ;
      pts[ctr,1] := x2 ;
      pts[ctr,2] := y2 ;
      flag := 1 ;
      x1 := x2 ;
      y1 := y2 ;
    end ;
  end ;
end ;

procedure Polygon_clipping ;
 type
  rec = record
       x1 , y1 : integer ;
       x2 , y2 : integer ;
       flag : boolean ;
    end ;
  arry = array [1..20] of rec ;

 var
   i , j , topx , topy , bottomx , bottomy , retval  : integer ;
   m , x , y , intersection : real ;
   arr : arry ;
   x1 , y1 , x2 , y2 , flag , ctr : integer ;
   c : char ;

procedure draw_win ;
 begin
   draw(topx,topy,bottomx,topy,3) ;
   draw(bottomx,topy,bottomx,bottomy,3) ;
   draw(bottomx,bottomy,topx,bottomy,3) ;
   draw(topx,bottomy,topx,topy,3) ;
 end ;

procedure clip_it ;
 var
   i : integer ;

   procedure above ;
    begin
      for i := 1 to ctr do
       begin
         if ( ( arr[i].y1 <= topy ) and ( arr[i].y2 <= topy ) ) then
           arr[i].flag := false
         else
           if ( not (( arr[i].y1 >= topy ) and ( arr[i].y2 >= topy )) ) then
            begin
              intersection := (topy - arr[i].y1) / (arr[i].y2 - arr[i].y1) * (arr[i].x2 - arr[i].x1) + arr[i].x1 ;
              if ( arr[i].y1 <= arr[i].y2 ) then
               begin
                 arr[i].y1 := topy ;
                 arr[i].x1 := round(intersection) ;
               end
              else
               if (arr[i].y2 <= arr[i].y1) then
                 begin
                   arr[i].y2 := topy ;
                   arr[i].x2 := round(intersection) ;
                 end ;
            end ;
       end ;
    end ;

   procedure below ;
    begin
      for i := 1 to ctr do
       begin
         if (( arr[i].y1 >= bottomy ) and ( arr[i].y2 >= bottomy )) then
           arr[i].flag := false
         else
           if ( not (( arr[i].y1 <= bottomy ) and ( arr[i].y2 <= bottomy )) ) then
            begin
              intersection := (bottomy - arr[i].y1) / (arr[i].y2 - arr[i].y1) * (arr[i].x2 - arr[i].x1) + arr[i].x1 ;
              if ( arr[i].y1 >= arr[i].y2 ) then
               begin
                 arr[i].y1 := bottomy ;
                 arr[i].x1 := round(intersection)
               end
              else
               if ( arr[i].y1 <= arr[i].y2 ) then
                begin
                  arr[i].y2 := bottomy ;
                  arr[i].x2 := round(intersection) ;
                end ;
            end ;
       end ;
    end ;

   procedure left ;
    begin
      for i := 1 to ctr do
       begin
         if (( arr[i].x1 <= topx ) and ( arr[i].x2 <= topx )) then
           arr[i].flag := false
         else
           if ( not (( arr[i].x1 >= topx ) and ( arr[i].x2 >= topx )) ) then
            begin
              intersection := (topx - arr[i].x1) / (arr[i].x2 - arr[i].x1) * (arr[i].y2 - arr[i].y1) + arr[i].y1 ;
              if ( arr[i].x1 <= arr[i].x2 ) then
               begin
                 arr[i].x1 := topx ;
                 arr[i].y1 := round(intersection) ;
               end
              else
               if (arr[i].x2 <= arr[i].x1) then
                begin
                  arr[i].x2 := topx ;
                  arr[i].y2 := round(intersection) ;
                end ;
            end ;
       end ;
    end ;

   procedure right ;
    begin
      for i := 1 to ctr do
       begin
         if (( arr[i].x1 >= bottomx ) and ( arr[i].x2 >= bottomx )) then
           arr[i].flag := false
         else
           if ( not (( arr[i].x1 <= bottomx ) and ( arr[i].x2 <= bottomx )) ) then
            begin
              intersection := (bottomx - arr[i].x1) / (arr[i].x2 - arr[i].x1) * (arr[i].y2 - arr[i].y1) + arr[i].y1 ;
              if ( arr[i].x2 <= arr[i].x1 ) then
               begin
                 arr[i].x1 := bottomx ;
                 arr[i].y1 := round(intersection) ;
               end
              else
               if (arr[i].x1 <= arr[i].x2) then
                begin
                  arr[i].x2 := bottomx ;
                  arr[i].y2 := round(intersection) ;
                end ;
            end ;
       end ;
    end ;

  begin
   for i := 1 to ctr do
     arr[i].flag := true ;
   clearscreen ;
   draw_win ;
   above ;
   below ;
   left ;
   right ;
   for i := 1 to ctr do
      if ( arr[i].flag ) then
         draw(arr[i].x1,arr[i].y1,arr[i].x2,arr[i].y2,3) ;
 end ;

begin  { clip }
  retval := 0 ;
  x1 := 160 ;
  x2 := x1 ;
  y1 := 100 ;
  y2 := y1 ;
  ctr := 0 ;
  clearscreen ;
  c := '0' ;
  while ( ord(c) <> 27 ) do
   begin
    plot(x2,y2,3) ;
    printcoor(x2,y2) ;
    read(kbd,c) ;
    c := upcase(c) ;
    if ( c = 'E' ) then
     begin
       for i := 1 to ctr do
         arr[i].flag := true ;
       clearscreen ;
       ctr := 0 ;
       draw_win ;
     end ;
    if ( c = 'C' ) then
     begin
       draw_win ;
       arr[ctr].x2 := arr[1].x1 ;
       arr[ctr].y2 := arr[1].y1 ;
       for i := 1 to ctr do
          draw(arr[i].x1,arr[i].y1,arr[i].x2,arr[i].y2,3) ;
       readln ;
       clip_it ;
     end ;
    case c of
      '8' : begin
              if ( y2 > 11 ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 y2 := y2 - 1 ;
               end ;
            end ;
      '2' : begin
              if ( y2 < 199 ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 y2 := y2 + 1 ;
               end ;
            end ;
      '4' : begin
              if ( x2 > 0 ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 x2 := x2 - 1 ;
               end ;
            end ;
      '6' : begin
              if ( x2 < 319 ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 x2 := x2 + 1 ;
               end ;
            end ;
      '7' : begin
              if ( ( y2 > 11 ) and ( x2 > 0 ) ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 y2 := y2 - 1 ;
                 x2 := x2 - 1 ;
               end ;
            end ;
      '9' : begin
              if ( ( y2 > 11 ) and ( x2 < 319 ) ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 y2 := y2 - 1 ;
                 x2 := x2 + 1 ;
               end ;
            end ;
      '1' : begin
              if ( ( y2 < 199 ) and ( x2 > 0 ) ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 y2 := y2 + 1 ;
                 x2 := x2 - 1 ;
               end ;
            end ;
      '3' : begin
              if ( ( y2 < 199 ) and ( x2 < 319 ) ) then
               begin
                 if ( flag = 0 ) then
                    plot(x2,y2,0) ;
                 flag := 0 ;
                 y2 := y2 + 1 ;
                 x2 := x2 + 1 ;
               end ;
            end ;
     end ;
   if ( ord(c) = 13 ) then
    begin
      plot(x2,y2,3) ;
      flag := 1 ;
      x1 := x2 ;
      y1 := y2 ;
      if ( retval = 3 ) then
       begin
         ctr := ctr + 1 ;
         arr[ctr-1].x2 := x1 ;
         arr[ctr-1].y2 := y1 ;
         arr[ctr].x1 := x1 ;
         arr[ctr].y1 := y1 ;
         retval := 3 ;
       end ;
      if ( retval = 2 ) then
       begin
         ctr := ctr + 1 ;
         arr[ctr].x1 := x1 ;
         arr[ctr].y1 := y1 ;
         retval := 3 ;
       end ;
      if ( retval = 1 ) then
       begin
         bottomx := x1 ;
         bottomy := y1 ;
         retval := 2 ;
         draw_win ;
       end ;
      if ( retval = 0 ) then
       begin
         topx := x1 ;
         topy := y1 ;
         retval := 1 ;
       end ;
    end ;
  end ;
end ;

begin
  read(kbd,c) ;
  c := upcase(c) ;
  if ( c = 'B' ) then
    Bezier_curve ;
  if ( c = 'S' ) then
    Bspline_curve ;
  if ( c = 'C' ) then
    Polygon_clipping ;
end ;
